package api

import (
	"encoding/json"
	"gcrontab/common"
	jobManager "gcrontab/master/job"
	"net/http"
)

//添加
func handleJobSave(resp http.ResponseWriter, req *http.Request) {
	var (
		job     common.Job
		oldJob  *common.Job
		postJob string
		bytes   []byte
		err     error
	)
	//解析post表单
	if err = req.ParseForm(); err != nil {
		goto ERR
	}
	//取字段
	postJob = req.PostForm.Get("job")
	//反序列号
	if err = json.Unmarshal([]byte(postJob), &job); err != nil {
		goto ERR
	}
	//存入etcd
	oldJob, err = jobManager.G_jobMgr.SaveJob(&job)
	if err != nil {
		goto ERR
	}

	//序列化response
	bytes, err = common.BuildResponse(0, "success:", oldJob)
	if err != nil {
		goto ERR
	}
	common.SendResponse(resp, bytes)
	return
ERR:
	bytes, _ = common.BuildResponse(0, "error:"+err.Error(), nil)
	common.SendResponse(resp, bytes)
}

//删除
func handleJobDelete(resp http.ResponseWriter, req *http.Request) {
	var (
		jobName string
		oldJobs []*common.Job
		err     error
		bytes   []byte
	)
	//解析post表单
	if err = req.ParseForm(); err != nil {
		goto ERR
	}
	//获取字段
	jobName = req.PostForm.Get("name")
	//删除etcd的job
	oldJobs, err = jobManager.G_jobMgr.DeleteJob(jobName)
	if err != nil {
		goto ERR
	}
	//序列化response
	bytes, err = common.BuildResponse(0, "success:", oldJobs)
	if err != nil {
		goto ERR
	}
	common.SendResponse(resp, bytes)
	return
ERR:
	bytes, _ = common.BuildResponse(0, "error:"+err.Error(), nil)
	common.SendResponse(resp, bytes)
}

//获取任务列表
func handleGetJobList(resp http.ResponseWriter, req *http.Request) {
	var (
		jobList []*common.Job
		err     error
		bytes   []byte
	)
	//获取job列表
	jobList, err = jobManager.G_jobMgr.GetJobList()
	if err != nil {
		goto ERR
	}
	//序列化response
	bytes, err = common.BuildResponse(0, "success:", jobList)
	if err != nil {
		goto ERR
	}
	common.SendResponse(resp, bytes)
	return
ERR:
	bytes, _ = common.BuildResponse(0, "error:"+err.Error(), nil)
	common.SendResponse(resp, bytes)
}

//强制关闭任务
func handleKillJob(resp http.ResponseWriter, req *http.Request) {
	var (
		name  string
		err   error
		bytes []byte
	)
	//解析post表单
	if err = req.ParseForm(); err != nil {
		goto ERR
	}
	name = req.PostForm.Get("name")
	//获取job列表
	err = jobManager.G_jobMgr.KillJob(name)
	if err != nil {
		goto ERR
	}
	//序列化response
	bytes, err = common.BuildResponse(0, "success:", nil)
	if err != nil {
		goto ERR
	}
	common.SendResponse(resp, bytes)
	return
ERR:
	bytes, _ = common.BuildResponse(0, "error:"+err.Error(), nil)
	common.SendResponse(resp, bytes)
}

//日志记录
func handJobLog(resp http.ResponseWriter, req *http.Request) {
	var (
		err        error
		name       string
		skipParam  string
		limitParam string
		skip       int64
		limit      int64
		logArr     []*common.JobLog
		bytes      []byte
	)
	//解析post表单
	if err = req.ParseForm(); err != nil {
		goto ERR
	}
	name = req.Form.Get("name")
	skipParam = req.Form.Get("skip") //Get返回string,需转int
	limitParam = req.Form.Get("limit")
	skip = common.StrToInt(skipParam, 0)
	limit = common.StrToInt(limitParam, 20)
	logArr, err = jobManager.G_logManager.GetLogList(name, skip, limit)
	if err != nil {
		goto ERR
	}
	//序列化response
	bytes, err = common.BuildResponse(0, "success:", logArr)
	if err != nil {
		goto ERR
	}
	common.SendResponse(resp, bytes)
	return
ERR:
	bytes, _ = common.BuildResponse(0, "error:"+err.Error(), nil)
	common.SendResponse(resp, bytes)
}

//获取获取register列表列表
func handleRegisterList(resp http.ResponseWriter, req *http.Request) {
	var (
		list  []string
		err   error
		bytes []byte
	)
	//获取register列表
	list, err = jobManager.G_jobMgr.GetRegisterList()
	if err != nil {
		goto ERR
	}
	//序列化response
	bytes, err = common.BuildResponse(0, "success:", list)
	if err != nil {
		goto ERR
	}
	common.SendResponse(resp, bytes)
	return
ERR:
	bytes, _ = common.BuildResponse(0, "error:"+err.Error(), nil)
	common.SendResponse(resp, bytes)
}
